import HTTP_CODE from '../constant/http-code'
import * as request from '../constant/fetch-request'
import { message } from 'antd'
import {convertContent} from '../constant/lang-util'

export const refreshTaskInfo = taskInfo => ({
  type: 'REFRESH_TASK',
  taskInfo
})

export const refreshFeeling = taskFeeling => ({
  type: 'REFRESH_FEELING',
  taskFeeling
})
export const refreshAssignments = assignments => ({
  type: 'REFRESH_ASSIGNMENTS',
  assignments
})

export const getTaskInfo = (taskId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/tasks/${taskId}/assignments/visibility`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshTaskInfo(res.body))
      }
    })()
  }
}

export const getAssignments = (taskId, studentId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/tutor/students/${studentId}/tasks/${taskId}/assignments`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshAssignments(res.body))
      }
    })()
  }
}

export const getStudentTaskAssignments = (taskId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/tasks/${taskId}/assignments`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshAssignments(res.body))
      }
    })()
  }
}

export const getTaskFeeling = (taskId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/tasks/${taskId}/feelings`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshFeeling(res.body))
      }
    })()
  }
}
export const addTaskFeeling = (taskId, feeling) => {
  return dispatch => {
    (async () => {
      const res = await request.post(`../api/feelings`, feeling)
      if (res.status === HTTP_CODE.CREATED) {
        dispatch(getTaskFeeling(taskId))
      }
    })()
  }
}

export const addTaskSuggestionContent = (taskId, suggestionContent) => {
  return dispatch => {
    (async () => {
      const res = await request.post(`../api/tasks/${taskId}/suggestions`, suggestionContent)
      if (res.status === HTTP_CODE.CREATED) {
        message.success(convertContent('提交成功'))
      }
    })()
  }
}

export const getFlipTask = (id) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/task/${id}/flip`)
      if (res.status === HTTP_CODE.OK) {
        dispatch({type: 'FLIP_PAGE_TASK_ID', ids: res.body})
      }
    })()
  }
}

export const getTasksContentReadByTaskId = (taskId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/tasks/${taskId}/contentRead`)
      if (res.status === HTTP_CODE.OK) {
        dispatch({type: 'REFRESH_TASK_CONTENT_READ', taskContentRead: res.body})
      }
    })()
  }
}

export const addTasksContentRead = (taskId, feeling) => {
  return dispatch => {
    (async () => {
      const res = await request.post(`../api/tasks/${taskId}/contentRead`, feeling)
      if (res.status === HTTP_CODE.CREATED) {
        message.success(convertContent('完成阅读'))
        dispatch(getTasksContentReadByTaskId(taskId))
      }
    })()
  }
}
